package com.aaa.security;

import com.aaa.dao.UserDao;
import com.aaa.entity.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户详情配置类
 */
@Component
public class UserDetailsConfig implements UserDetailsService {

    @Resource
    UserDao userDao;

    /**
     * 根据用户名加载用户详细信息
     *
     * @param name:提交的用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        System.out.println("用户名：" + name);
        // 根据用户名查询用户相信信息
        List<User> tuserDetails = userDao.login(name);
        Integer rid=tuserDetails.get(0).getRid();

        if(tuserDetails.size()==1){
//            String role=userDao.selectRole(rid).get(0).getRole();
//            String priviledge=userDao.selectRole(rid).get(0).getPriviledge();
            // 查询角色,查询权限
            //tuserDetails.get(0).setAuthorities(role,priviledge);
            return tuserDetails.get(0);
        }else{
            throw new UsernameNotFoundException("用户名查询不到");
        }
    }
}
